<?php
/**
 * Created by PhpStorm.
 * User:Yishoujian
 * Date: 2018/11/28
 * Time: 18:32
 */

namespace app\index\controller;


use EasyWeChat\Foundation\Application;
use think\Controller;

class Server extends Controller
{
    public function call()
    {

        $app = new Application(config("wx"));
        $oauth = $app->oauth;
        $server=$app->server;
//        $server->setMessageHandler(function ($message) {
//          if ($message->EventKey=="key_today"){
//          return view("index/index");
//          }
//        });




// 获取 OAuth 授权结果用户信息
        $user = $oauth->user();

//        $_SESSION['wechat_user'] = $user->toArray();
        session("wechat_user", $user->toArray());

//        $targetUrl = empty($_SESSION['target_url']) ? '/' : $_SESSION['target_url'];
        $targetUrl = session("target_url") ?? '/';

//        header('location:'. $targetUrl); // 跳转到 user/profile
        return redirect($targetUrl);

    }


    public function getMenu()
    {

        $app = new Application(config('wx'));

        //2.操作菜单的对象
        $menu = $app->menu;
        //3.得到所有菜单
        $menu->all();
    }


    public function setMenu()
    {
        $buttons = [
            [
                "type" => "view",
                "name" => "首页",
                "url" => "http://wuye.ysj1.cn/index/index/index"
            ],
            [
                "name" => "小区服务",
                "sub_button" => [


                    [
                        "type" => "view",
                        "name" => "小区通知",
                        "url" => "http://wuye.ysj1.cn/index/notice/index"
                    ],

                    [
                        "type" => "view",
                        "name" => "在线维修",
                        "url" => "http://wuye.ysj1.cn/index/online/index"
                    ],
                    [
                        "type" => "view",
                        "name" => "个人信息",
                        "url" => "http://wuye.ysj1.cn/index/user/index"
                    ],


                ],
            ],
        ];
        $app = new Application(config('wx'));

        //2.操作菜单的对象
        $menu = $app->menu;
        $menu->add($buttons);
    }





}